package store.gn998.stream;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Author 郭泽鹏
 * @Date 2021/11/8 10:39
 * @Version 1.0
 * @Description 创建流
 */
public class StreamCreate {

    private static final List<String> list = Arrays.asList("a", "b", "c");

    /**
     * 通过 java.util.Collection.stream() 方法用集合创建流
     */
    public void test01() {
        //创建一个顺序流
        Stream<String> stream = list.stream();
        //创建一个并行流
        Stream<String> parallelStream = list.parallelStream();
    }

    /**
     * 使用 java.util.Arrays.stream(T[] array) 方法用数组创建流
     */
    public void test02() {
        int[] array = {1, 3, 5, 6, 8};
        IntStream stream = Arrays.stream(array);
    }

    /**
     * 使用 Stream 静态方法 of, iterate, generate 方法创建流
     */
    @Test
    public void test03() {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
        stream.forEach(System.out::println);

        System.out.println("--------------");
        Stream<Integer> stream1 = Stream.iterate(0, x -> (x + 3)).limit(4);
        stream1.forEach(System.out::println);

        System.out.println("--------------");
        Stream<Double> stream2 = Stream.generate(Math::random).limit(5);
        stream2.forEach(System.out::println);
    }

    @Test
    public void test04() {
        Stream<String> stream = list.stream().parallel().filter(x -> x.compareTo("b") >= 0);
        stream.forEach(System.out::println);
    }
}
